package com.caiyigo.baihua.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.caiyigo.baihua.Const.UrlConfig;
import com.caiyigo.baihua.dto.Result;
import com.caiyigo.baihua.dto.Tree;
import com.caiyigo.baihua.model.SystemRes;
import com.caiyigo.baihua.service.ResService;
import com.caiyigo.baihua.service.RoleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping(value = "/system")
public class SystemController {

	private ResService resService;
	@Autowired
	private RoleService roleService;

	public ResService getResService() {
		return resService;
	}

	@Autowired
	public void setResService(ResService resService) {
		this.resService = resService;
	}

	/**
	 * 左侧菜单树
	 * 
	 * @param id
	 * @param passId
	 * @param type
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping(value = "/res/tree", method = RequestMethod.POST)
	public @ResponseBody String tree(
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "passId", required = false) Integer passId,
			@RequestParam(value = "type", required = false, defaultValue = "1") Integer type)
			throws JsonProcessingException {

		ObjectMapper objectMapper = new ObjectMapper();
		List<Tree> trees = resService.getTree(id, type, passId);
		// 去除无子节点的菜单
		List<Tree> removeList = new ArrayList<Tree>();
		for (Tree tree : trees) {
			if (tree.getChildren().size() == 0) {
				removeList.add(tree);
			}
		}
		if (removeList.size() > 0) {
			trees.removeAll(removeList);
		}
		return objectMapper.writeValueAsString(trees);
	}

	@RequestMapping(value = "/res", method = RequestMethod.GET)
	public String res() {
		return UrlConfig.SYSTEM.RES;
	}

	@RequestMapping(value = "/res/add", method = RequestMethod.POST)
	public @ResponseBody Result add(@ModelAttribute("res") SystemRes res)
			throws Exception {

		return new Result(resService.add(res) > 0);

	}

	@RequestMapping(value = "/res/delete", method = RequestMethod.POST)
	public @ResponseBody Result delete(int id) throws Exception {

		return new Result(resService.deleteByIdAndPid(id));

	}

	@RequestMapping(value = "/res/edit", method = RequestMethod.POST)
	public @ResponseBody Result edit(SystemRes res) throws Exception {
		return resService.edit(res);
	}

	@RequestMapping(value = "/res/list", method = RequestMethod.POST)
	public @ResponseBody String resList(
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "passId", required = false) Integer passId,
			@RequestParam(value = "type", required = false, defaultValue = "1") Integer type)
			throws JsonProcessingException {
		ObjectMapper objectMapper = new ObjectMapper();
		return objectMapper.writeValueAsString(resService.listBySeq());
	}
}
